package com.zkthink.ceres.applet.authority.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import com.zkthink.base.R;
import com.zkthink.ceres.applet.authority.domain.entity.AppletMember;
import com.zkthink.ceres.applet.authority.mapper.AppletMemberMapper;
import com.zkthink.ceres.applet.authority.service.AppletMemberService;
import com.zkthink.ceres.applet.user.vo.CustomerVo;
import com.zkthink.ceres.member.entity.Member;
import com.zkthink.ceres.member.service.MemberService;
import com.zkthink.clh.order.dto.CouponsPageDTO;
import com.zkthink.clh.order.entity.AppletVip;
import com.zkthink.clh.order.entity.ChlCoupons;
import com.zkthink.context.BaseContextHandler;
import com.zkthink.zhl.order.dao.ChlCouponsMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**

@Author cmz
@Date 2024-04-29 16:39
*/
@Service
public class AppletMemberServiceImpl extends ServiceImpl<AppletMemberMapper, AppletMember> implements AppletMemberService{
    @Resource
    private AppletMemberMapper appletMemberMapper;

    @Resource
    private ChlCouponsMapper chlCouponsMapper;

    @Resource
    private MemberService memberService;


    @Override
    public AppletMember findByAccount(String phone) {
        if (StringUtils.isNullOrEmpty(phone)){
            return new AppletMember();
        }else{
            QueryWrapper<AppletMember> appletMemberQueryWrapper = new QueryWrapper<>();
            appletMemberQueryWrapper.eq("phone", phone);
            return appletMemberMapper.selectOne(appletMemberQueryWrapper);
        }
    }

    @Override
    public R getBalance() {
        Long mid = BaseContextHandler.getUserId();
        QueryWrapper<AppletMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",mid);
        AppletMember appletMember = appletMemberMapper.selectOne(queryWrapper);
        
        return R.success(appletMember.getBalance());
    }

    @Override
    public List<CustomerVo> selectCustomerList() {
        return appletMemberMapper.selectCustomer();
    }

    @Override
    public IPage<ChlCoupons> findCoupons(IPage page, CouponsPageDTO model) {
        return baseMapper.findCoupons(page, model);
    }

    @Override
    public List<AppletVip> selectVip() {
        return baseMapper.selectVip();
    }

    /**
     * 购买vip
     *
     * @param vipId
     * @return
     */
    @Override
    public R buyVip(String vipId) {
        AppletVip appletVip = appletMemberMapper.findVipById(vipId);
        if (appletVip == null) {
            return R.fail("vip套餐不存在");
        }
        //支付后回调参数逻辑，支付申请未完成，暂时放在购买里
        int vipDay = Integer.parseInt(appletVip.getPackageTime());
        Long mid = BaseContextHandler.getUserId();
        Member member = memberService.getById(mid);
        if (member.getVipTime() != null) {
            if (member.getVipTime().isAfter(LocalDateTime.now())) {
                member.setVipTime(member.getVipTime().plusDays(vipDay));
            } else {
                member.setVipTime(LocalDateTime.now().plusDays(vipDay));
            }
        } else {
            member.setVipTime(LocalDateTime.now().plusDays(vipDay));
        }
        member.setLevel(1);
        memberService.updateAllById(member);
//        //判断用户是否是VIP
//        if (member.getVipTime().isAfter(LocalDateTime.now())){
//            ChlCoupons chlCoupons = chlCouponsService.findRecharge(recharge);
//            if (chlCoupons != null) {
//                member.setCoupons(chlCoupons.getCoupons());
//            }
//            member.setBalance(recharge);
//            memberService.updateAllById(member);
//        }
        return R.success();
    }

    @Override
    public R buyChlCoupons(String chlCouponsId) {
        ChlCoupons chlCoupons = chlCouponsMapper.selectById(chlCouponsId);
        if (chlCoupons == null) {
            return R.fail("消费金套餐不存在");
        }
        //支付后回调参数逻辑，支付申请未完成，暂时放在购买里
        Long mid = BaseContextHandler.getUserId();
        Member member = memberService.getById(mid);
        member.setCoupons(member.getCoupons().add(chlCoupons.getCoupons()));
        memberService.updateAllById(member);
        return  R.success();
    }
}
